home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C03 / Mathops.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.2 KB  |  45 lines

  1. //: C03:Mathops.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Mathematical operators
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. // A macro to display a string and a value.
  11. #define PRINT(STR, VAR) \
  12.   cout << STR " = " << VAR << endl
  13.  
  14. int main() {
  15.   int i, j, k;
  16.   float u,v,w;  // Applies to doubles, too
  17.   cout << "enter an integer: ";
  18.   cin >> j;
  19.   cout << "enter another integer: ";
  20.   cin >> k;
  21.   PRINT("j",j);  PRINT("k",k);
  22.   i = j + k; PRINT("j + k",i);
  23.   i = j - k; PRINT("j - k",i);
  24.   i = k / j; PRINT("k / j",i);
  25.   i = k * j; PRINT("k * j",i);
  26.   i = k % j; PRINT("k % j",i);
  27.   // The following only works with integers:
  28.   j %= k; PRINT("j %= k", j);
  29.   cout << "Enter a floating-point number: ";
  30.   cin >> v;
  31.   cout << "Enter another floating-point number:";
  32.   cin >> w;
  33.   PRINT("v",v); PRINT("w",w);
  34.   u = v + w; PRINT("v + w", u);
  35.   u = v - w; PRINT("v - w", u);
  36.   u = v * w; PRINT("v * w", u);
  37.   u = v / w; PRINT("v / w", u);
  38.   // The following works for ints, chars, 
  39.   // and doubles too:
  40.   u += v; PRINT("u += v", u);
  41.   u -= v; PRINT("u -= v", u);
  42.   u *= v; PRINT("u *= v", u);
  43.   u /= v; PRINT("u /= v", u);
  44. } ///:~
  45.